﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Text;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Security;
using System.Security.Cryptography;
using Common;

public partial class membership_register_dialog : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    { 
        Response.Expires = -1;
        if (RequestBase.GetFormString("otype") == "membership_register")
        {
            RegisterSubmit();
        }
    }
    #region 验证
    void ValidateUsernameExisted()
    {
        bool _val = BLL.MembershipBase.ValidateUsernameExist(RequestBase.GetFormString("_value"));

        if (_val)
        {
            BLL.AjaxHelper.ResponseWrite("existed");
        }
        else
        {
            BLL.AjaxHelper.ResponseWrite("none");
        }
    }

    void ValidateEmailExist()
    {
        bool _val = BLL.MembershipBase.ValidateEmailExist(RequestBase.GetFormString("_value"));

        if (_val)
        {
            BLL.AjaxHelper.ResponseWrite("existed");
        }
        else
        {
            BLL.AjaxHelper.ResponseWrite("none");
        }
    }
    #endregion

    #region 提交注册信息
    /// <summary>
    /// 提交注册信息
    /// </summary>
    void RegisterSubmit()
    {
        Model.MembershipBase _userObj = new Model.MembershipBase();
        _userObj.UserName = RequestBase.GetFormString("txtUsername");
        _userObj.FullName = RequestBase.GetFormString("txtName");
        _userObj.MobileAlias = "";
        _userObj.Birthday = DateTime.Parse("1754-01-01");
        _userObj.Gender = -1;
        _userObj.RegionId = 0;
        _userObj.CountryId = 0;
        _userObj.ProvinceId = 0;
        _userObj.CityId = 0;
        _userObj.RegionInfo = ""; ;
        _userObj.Password = RequestBase.GetFormString("txtPassword");
        _userObj.MobilePIN = "";
        _userObj.Email = RequestBase.GetFormString("txtEmail");
        _userObj.PasswordQuestion = "";
        _userObj.PasswordAnswer = "";
        _userObj.IsApproved = true;
        _userObj.IsLockedOut = false;
        _userObj.CreateDate = DateTime.Now;
        _userObj.Comment = "";

        Model.Enum.MembershipCreateStatus status = Model.Enum.MembershipCreateStatus.ProviderError;
        Model.MembershipBase _obj = (new BLL.MembershipBase()).CreateUser(_userObj, out status);

        switch ((int)status)
        {
            case 0:
                BLL.SQLIPrincipal principal = new BLL.SQLIPrincipal(RequestBase.GetFormString("txtUsername"));
                // 如果用户通过验证,则将用户信息保存在缓存中
                Context.User = principal;
                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(RequestBase.GetFormString("txtUsername"), false, 1800);
                string encryptedTicket = FormsAuthentication.Encrypt(ticket);
                HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                CookieEncrypt.SetCookie(authCookie);
                Model.IntegralHistoryBase _integralobj = new Model.IntegralHistoryBase();
                _integralobj.UserId = _obj.UserId;
                _integralobj.IntegralCount = int.Parse(ConfigurationManager.AppSettings["IntegralByRegister"]);
                _integralobj.UserName = _obj.UserName;
                _integralobj.CreatedDate = DateTime.Now;
                _integralobj.ExpirationDate = DateTime.Now.AddDays(5);
                _integralobj.ComesFrom = "会员注册";
                _integralobj.IsValid = false;
                _integralobj.ValidIntegralCount = int.Parse(ConfigurationManager.AppSettings["IntegralByRegister"]);
                BLL.IntegralBase.CreateIntegral(_integralobj);
                BLL.AjaxHelper.ResponseWrite("succeeded");
                break;
            default:
                BLL.AjaxHelper.ResponseWrite("<li style=\"background: none;\">" + status.ToString() + "</li>");
                break;
        }
    }
    #endregion
}